package cn.edu.neusoft.yyd208.foodorder.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.edu.neusoft.yyd208.foodorder.R;
import cn.edu.neusoft.yyd208.foodorder.adapter.FoodListAdapter;
import cn.edu.neusoft.yyd208.foodorder.bean.CollectBean;
import cn.edu.neusoft.yyd208.foodorder.bean.FoodBean;
import cn.edu.neusoft.yyd208.foodorder.bean.RegisterBean;
import cn.edu.neusoft.yyd208.foodorder.listener.ListListener;
import cn.edu.neusoft.yyd208.foodorder.listener.TListener;
import cn.edu.neusoft.yyd208.foodorder.model.CollectShopModel;
import cn.edu.neusoft.yyd208.foodorder.model.FoodListModel;

public class FoodListActivity extends BaseActivity  {
    String id,shopname,flag;
    String userid;
    int flag1;
    List<FoodBean> items;
    RecyclerView recyclerView;
    ImageView reback, collect;
    TextView tv_id;
    FoodListModel foodListModel;
    CollectShopModel collectShopModel;


    ListListener<FoodBean> listListener=new ListListener<FoodBean>() {
        @Override
        public void onResponse(List<FoodBean> list) {
              items=list;
              FoodListAdapter myAdapter=new FoodListAdapter(FoodListActivity.this,items,R.layout.card);
              recyclerView.setAdapter(myAdapter);
              recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void onFail(String msg) {
        showToast("error");
    }
};
TListener<CollectBean> judgeListener=new TListener<CollectBean>() {
    @Override
    public void onResponse(CollectBean collectBean) {
        flag = collectBean.getCollected().toString();
        if (flag.equals("1")) {
            flag1 = 1;
            collect.setImageResource(R.drawable.ycollect);
            showToast("已收藏");
        }
        else {
            flag1=0;
            collect.setImageResource(R.drawable.collect);
            showToast("未收藏");
        }
    }
    @Override
    public void onFail(String msg) {

    }
};
    TListener<RegisterBean> collectListListener=new TListener<RegisterBean>() {
        @Override
        public void onResponse(RegisterBean registerBean) {
            if(registerBean.getSuccess().equals("1")){
                if(flag1==1){
                    collect.setImageResource(R.drawable.collect);
                    flag1=0;
                }
                else {
                    collect.setImageResource(R.drawable.ycollect);
                    flag1=1;
                }
                showToast("操作成功");

            }
            else {
                showToast("操作失败");
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    void initView() {
        setLayout_file(R.layout.activity_food_list);
        reback = (ImageView) findViewById(R.id.reback);
        collect = (ImageView) findViewById(R.id.collect);
        tv_id=(TextView)findViewById(R.id.tv_id);
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        shopname=intent.getStringExtra("shopname");
        tv_id.setText(shopname);
        showToast(shopname);
        userid=getUser_userid();


        recyclerView =(RecyclerView) findViewById(R.id.Foodrecyclerview);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

    }

    @Override
    void initEvent() {
        reback.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
               finish();
        }
     });
        collect.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                collectShopModel.collect(userid,id,collectListListener);

            }
        });
    }

    @Override
    void initData() {
        foodListModel=new FoodListModel();
        foodListModel.getFoodMessage(id,listListener);
        collectShopModel=new CollectShopModel();
        collectShopModel.judge(userid,id,"0",judgeListener);

    }

}